package com.ax.controller;



import com.ax.dto.EmployeeDTO;
import com.ax.dto.LoginDTO;
import com.ax.dto.EmployeePageQueryDTO;

import com.ax.properties.JwtProperties;

import com.ax.result.PageResult;
import com.ax.result.Result;

import com.ax.security.JwtUtil;
import com.ax.security.UserDetailsImpl;
import com.ax.security.UserDetailsServiceImpl;
import com.ax.service.EmployeeService;
import com.ax.vo.LoginVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.naming.AuthenticationException;


@RestController
@RequestMapping("api/auth/admin")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private AuthenticationManager authenticationManager;
    // 注入RedisTemplate
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private UserDetailsServiceImpl userDetailsServiceImpl;

    private void authenticate(String username, String password) throws AuthenticationException {
        try {
            authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(username, password)
            );
        } catch (BadCredentialsException e) {
            throw new AuthenticationException("用户名或密码错误");
        }
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) throws AuthenticationException {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        // 触发 Spring Security 认证
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(username, password)
        );
        UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();

        String token = jwtUtil.generateToken(userDetails);
        LoginVO loginVO = LoginVO.builder()
                .id(userDetails.getUser().getId())
                .userName(userDetails.getUsername())
                .name(userDetails.getUser().getName())
                .token(token)
                .build();
        log.info("员工登录：{}", loginDTO);
        return Result.success(loginVO);
    }
    @PostMapping("/register")
    @ApiOperation(value = "注册")
    public Result<String> register(@RequestBody EmployeeDTO employeeDTO){
        log.info("用户注册请求：用户名={}", employeeDTO.getUsername());
        employeeService.register(employeeDTO);
        return Result.success("注册成功，请使用账号密码登录");
    }

    @GetMapping("/pageQueryEmployee")
    @ApiOperation(value = "员工分页查询")
    public Result pageQueryEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询：{}", employeePageQueryDTO);

        PageResult result = employeeService.pageQueryEmployee(employeePageQueryDTO);
        return Result.success(result);
    }

    @PutMapping
    @ApiOperation(value = "编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息：{}", employeeDTO);

        employeeService.update(employeeDTO);
        return Result.success();
    }

    @PostMapping
    @ApiOperation(value = "添加员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("添加员工信息：{}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }


}
